package com.doremi.presentation.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

import com.doremi.R;
import com.doremi.service.play_songs.PlaySongsServiceAcces;

public class DoReMiWidgetProvider extends AppWidgetProvider {

	public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
	public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
	/***estados del boton play/pause************/
	public static final int _PLAY = 1;
	public static final int _PAUSE = 0;
	private static int state_button=_PLAY;
	/******mensajes internos del widget**********/
	private final String _TOUCH_PLAY="TOUCH_PLAY";
	private final String _TOUCH_FORDWARD="TOUCH_FORDWARD";
	private final String _TOUCH_BACKWARD="TOUCH_BACKWARD";
	private final String _TOUCH_NEXT="TOUCH_NEXT";
	private final String _TOUCH_PREVIOUS="TOUCH_PREVIOUS";
	/*******mensajes externos que indican actualizar el boton PLAY/PAUSE******/
	public static final String _STATE_BUTTON_IN_PLAY="Button looks like playing";
	public static final String _STATE_BUTTON_IN_PAUSED="Button looks like paused";
	

	public DoReMiWidgetProvider() {
		// Button playButton=(Button)fin
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.player_widget_layout);

		// remoteViews.setOnClickPendingIntent(R.id.btnPlay_widget, new
		// PendingIntent());
		// pushWidgetUpdate(context, remoteViews);

		Intent active1 = new Intent(context, DoReMiWidgetProvider.class);
		active1.setAction(ACTION_WIDGET_RECEIVER);
		active1.putExtra("msg", _TOUCH_PLAY);

		PendingIntent actionPendingIntent1 = PendingIntent.getBroadcast(context, 1, active1, 0);
		remoteViews.setOnClickPendingIntent(R.id.btnPlay_widget,
				actionPendingIntent1);

		Intent active2 = new Intent(context, DoReMiWidgetProvider.class);
		active2.setAction(ACTION_WIDGET_RECEIVER);
		active2.putExtra("msg", _TOUCH_FORDWARD);

		PendingIntent actionPendingIntent2 = PendingIntent.getBroadcast(context, 2, active2, 0);
		remoteViews.setOnClickPendingIntent(R.id.btnForward_widget,
				actionPendingIntent2);

		Intent active3 = new Intent(context, DoReMiWidgetProvider.class);
		active3.setAction(ACTION_WIDGET_RECEIVER);
		active3.putExtra("msg", _TOUCH_BACKWARD);

		PendingIntent actionPendingIntent3 = PendingIntent.getBroadcast(context, 3, active3, 0);
		remoteViews.setOnClickPendingIntent(R.id.btnBackward_widget,
				actionPendingIntent3);
		
		Intent active4 = new Intent(context, DoReMiWidgetProvider.class);
		active4.setAction(ACTION_WIDGET_RECEIVER);
		active4.putExtra("msg", _TOUCH_NEXT);

		PendingIntent actionPendingIntent4 = PendingIntent.getBroadcast(context, 4, active4, 0);
		remoteViews.setOnClickPendingIntent(R.id.btnNext_widget,
				actionPendingIntent4);
		
		Intent active5 = new Intent(context, DoReMiWidgetProvider.class);
		active5.setAction(ACTION_WIDGET_RECEIVER);
		active5.putExtra("msg", _TOUCH_PREVIOUS);

		PendingIntent actionPendingIntent5 = PendingIntent.getBroadcast(context, 5, active5, 0);
		remoteViews.setOnClickPendingIntent(R.id.btnPrevious_widget,
				actionPendingIntent5);
		

		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
	}

	public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
		ComponentName myWidget = new ComponentName(context,	DoReMiWidgetProvider.class);
		AppWidgetManager manager = AppWidgetManager.getInstance(context);
		manager.updateAppWidget(myWidget, remoteViews);
	}

	@Override
	public void onReceive(Context context, Intent intent) {

		final String action = intent.getAction();
		if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
			final int appWidgetId = intent.getExtras().getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
				this.onDeleted(context, new int[] { appWidgetId });
			}
		} else {
			/*********touchs en el widget*****************************************/
			if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
				String msg = "null";
				try {
					msg = intent.getStringExtra("msg");
				} catch (NullPointerException e) {
					Log.e("Error", "msg = null");
				}

				//Toast.makeText(context, "Out: " + msg, Toast.LENGTH_SHORT)
					//	.show();
				Log.v("Widget",msg);
				if (msg.equals(_TOUCH_PLAY)) {
					if(state_button==_PLAY){
					  PlaySongsServiceAcces.playService(context);
					}
					else{
					  PlaySongsServiceAcces.pauseService(context);
					}
				}
				else
				if (msg.equals(_TOUCH_FORDWARD)) 
				{
					 PlaySongsServiceAcces.fowardService(context);
				}
				else
			    if (msg.equals(_TOUCH_BACKWARD))
				{
			    	PlaySongsServiceAcces.backwardService(context);
				}
				else
				if (msg.equals(_TOUCH_NEXT)) 
				{
					 PlaySongsServiceAcces.nextService(context);
				}
				else
				if (msg.equals(_TOUCH_PREVIOUS))
				{
				   	PlaySongsServiceAcces.previousService(context);
				}
				/***********mensajes provenientes del PlaySOngsService**********************/
				else
				if (msg.equals(DoReMiWidgetProvider._STATE_BUTTON_IN_PAUSED)) {
					RemoteViews newView = new RemoteViews(
							context.getPackageName(),
							R.layout.player_widget_layout);

					newView.setImageViewResource(R.id.btnPlay_widget,
							R.drawable.btn_pause);
					ComponentName thisWidget = new ComponentName(context,
							DoReMiWidgetProvider.class);
					AppWidgetManager manager = AppWidgetManager
							.getInstance(context);
					manager.updateAppWidget(thisWidget, newView);
					state_button=_PAUSE;
				}
				else
				if (msg.equals(DoReMiWidgetProvider._STATE_BUTTON_IN_PLAY)) {
					RemoteViews newView = new RemoteViews(
							context.getPackageName(),
							R.layout.player_widget_layout);

					newView.setImageViewResource(R.id.btnPlay_widget,
							R.drawable.btn_play);
					ComponentName thisWidget = new ComponentName(context,
							DoReMiWidgetProvider.class);
					AppWidgetManager manager = AppWidgetManager
							.getInstance(context);
					manager.updateAppWidget(thisWidget, newView);
					state_button=_PLAY;
				}
			}
			super.onReceive(context, intent);
		}
	}

	

}